package penguin.masking;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class RedisCache implements DataReductionCache {

    ContextConfigLoad configLoad = ContextConfigLoad.configLoad();
    String ip = configLoad.getProperty("redis.ip");
    int port = Integer.parseInt(configLoad.getProperty("redis.port"));
    int db = Integer.parseInt(configLoad.getProperty("redis.db"));
    String prefix = configLoad.getProperty("redis.prefix");
    int maxTotal = Integer.parseInt(configLoad.getProperty("redis.maxTotal"));
    int maxIdle = Integer.parseInt(configLoad.getProperty("redis.maxIdle"));
    String user = configLoad.getProperty("redis.user");
    String pwd = configLoad.getProperty("redis.pwd");
    volatile JedisPool jedisPool = null;

    public Jedis connect() {
        if (jedisPool == null) {
            synchronized (RedisCache.this) {
                if (jedisPool == null) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    //最大连接数
                    config.setMaxTotal(maxTotal);
                    //最大空闲连接数
                    config.setMaxIdle(maxIdle);
                    //获得连接池
                    jedisPool = new JedisPool(config, ip, port);
                }
            }
        }
        Jedis jedis = jedisPool.getResource();
        if (user != null) {
            jedis.auth(user, pwd);
        } else if (pwd != null) {
            jedis.auth(pwd);
        }
        jedis.select(db);
        return jedis;
    }

    @Override
    public String addNode(String key, String value) {
        Jedis jedis = connect();
        String _key = redisKey(key);
        String _value = valueKey(value);
        Logger.info(_key + "：" + key + ", " + _value + " ：" + value);
        jedis.set(_key, value);
        jedis.set(_value, key);
        jedis.set(idKey(), key);
        jedis.close();
        return key;
    }

    @Override
    public String value(String key) {
        Jedis jedis = connect();
        String _key = redisKey(key);
        String v = jedis.get(_key);
        jedis.close();
        return v;
    }

    @Override
    public String key(String value) {
        Jedis jedis = connect();
        String _key = valueKey(value);
        String k = jedis.get(_key);
        jedis.close();
        return k;
    }

    @Override
    public long initId() {
        Jedis jedis = connect();
        String id = jedis.get(idKey());
        jedis.close();
        return id == null ? 1 : Long.parseLong(id);
    }

    @Override
    public void free() {
        boolean closed = jedisPool.isClosed();
        if (!closed) {
            jedisPool.close();
            jedisPool = null;
        }
    }

    private String idKey() {
        return prefix + ":id";
    }

    private String redisKey(String key) {
        return prefix + ":key:" + key;
    }

    private String valueKey(String key) {
        return prefix + ":value:" + StringUtil.mdfIfLong(key);
    }
}
